package com.oss.service.oss;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oss.dto.oss.DeleteByIdDto;
import com.oss.dto.oss.capitalsummary.PageDto;
import com.oss.dto.oss.rechargeManage.AddDto;
import com.oss.dto.oss.rechargeManage.UpdateDto;
import com.oss.entity.oss.RechargeManage;
import com.oss.enums.IsDeletedEnum;
import com.oss.mapper.oss.RechargeManageMapper;
import com.oss.service.system.DictionaryService;
import com.oss.service.system.UserService;
import com.oss.util.SnowFlakeUtil;
import com.oss.vo.oss.rechargeManage.RechargeManageVO;
import com.oss.vo.oss.student.StudentVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RechargeManageService extends ServiceImpl<RechargeManageMapper, RechargeManage> {
    @Autowired
    StudentService studentService;
    @Autowired
    UserService userService;
    @Autowired
    DictionaryService dictionaryService;

    public IPage<RechargeManageVO> selectByPage(PageDto dto) {
        LambdaQueryWrapper<RechargeManage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RechargeManage::getIsDeleted, IsDeletedEnum.NO.getFlag());
        wrapper.eq(StringUtils.isNotBlank(dto.getStudentID()),RechargeManage::getStudentID,dto.getStudentID());
        IPage<RechargeManage> page = this.page(new Page<>(dto.getPageNo(), dto.getPageSize()), wrapper);
        Map<String, StudentVO> studentMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(page.getRecords())){
            Set<String> ids = page.getRecords().stream().map(RechargeManage::getStudentID).collect(Collectors.toSet());
            studentMap = studentService.selectByIDs(ids);
        }
        Map<String, StudentVO> finalStudentMap = studentMap;
        Map<String,String> userMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            // 获取创建人和修改人ID
            Set<String> ids = page.getRecords().stream().map(RechargeManage::getCreatorId).collect(Collectors.toSet());
            ids.addAll(page.getRecords().stream().map(RechargeManage::getUpdatorId).collect(Collectors.toSet()));
            userMap = userService.selectByIds(ids);
        }
        Map<String, String> finalUserMap = userMap;
        return page.convert(entity->{
            RechargeManageVO vo = new RechargeManageVO();
            BeanUtils.copyProperties(entity,vo);
            if (finalStudentMap.containsKey(entity.getStudentID())){
                StudentVO studentVO = finalStudentMap.get(entity.getStudentID());
                vo.setStudentID(studentVO.getId());
                vo.setStudentPhone(studentVO.getPhone());
                vo.setStudentName(studentVO.getName());
                vo.setStudentWechatID(studentVO.getWechatID());
                vo.setStudentAccount(studentVO.getAccount());
            }
            vo.setCreatorName(finalUserMap.get(entity.getCreatorId()));
            vo.setUpdatorName(finalUserMap.get(entity.getUpdatorId()));
            return vo;
        });
    }

    public String add(AddDto dto) {
        RechargeManage entity = new RechargeManage();
        BeanUtils.copyProperties(dto,entity);
        entity.setId(SnowFlakeUtil.getID());
        this.save(entity);
        return "操作成功";
    }

    public String updateData(UpdateDto dto) {
        RechargeManage entity = new RechargeManage();
        BeanUtils.copyProperties(dto,entity);
        this.updateById(entity);
        return "操作成功";
    }

    public String delete(DeleteByIdDto dto) {
        LambdaUpdateWrapper<RechargeManage> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(RechargeManage::getId,dto.getId());
        lambdaUpdateWrapper.set(RechargeManage::getIsDeleted,IsDeletedEnum.YES.getFlag());
        this.update(null,lambdaUpdateWrapper);
        return "操作成功";
    }

    public List<RechargeManage> getByStudentID(String studentID) {
        LambdaQueryWrapper<RechargeManage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RechargeManage::getIsDeleted, IsDeletedEnum.NO.getFlag());
        wrapper.eq(RechargeManage::getStudentID,studentID);
        return this.list(wrapper);
    }
}